package sz.szsmk.citizencard.ui.popwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ui.adapter.CalenderForPopWindowAdapter;

/**
 * author : hyt
 * date : 2019/8/19
 * description :
 */
public class CalendarPopWindow extends PopupWindow{

    View mPopView;

    RecyclerView recycle_list;
    private OnItemClickListener mListener;
    private Context context;

    public CalendarPopWindow(final Context context) {
        super(context);
        this.context = context;
        initView(context);
        setPopupWindow();
    }

    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mPopView = inflater.inflate(R.layout.popurwindow_item_calender, null);
        recycle_list  = mPopView.findViewById(R.id.recycle_list);
        CalenderForPopWindowAdapter adapter = new CalenderForPopWindowAdapter(context);
        recycle_list.setLayoutManager(new LinearLayoutManager(context));
        recycle_list.setHasFixedSize(true);
        recycle_list.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
        recycle_list.setAdapter(adapter);
        adapter.setOnItemClickListener(new CalenderForPopWindowAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String data) {
                mListener.setOnItemClick(data);
                dismiss();
            }
        });


     }

    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
//        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的宽

        this.setHeight(600);// 设置弹出窗口的高

        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, -15, 10);
        } else {
            this.dismiss();
        }
    }



    /**
     * 定义一个接口，公布出去 在Activity中操作按钮的单击事件
     */
    public interface OnItemClickListener {
        void setOnItemClick(String data);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

}